﻿using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;
using System.Text;

namespace FreeBroker
{
    public partial class DetailView : Form
    {
        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.chartDocument.Print();
        }

        private void buttonPrintPreview_Click(object sender, EventArgs e)
        {
            DialogResult result = this.printPreviewDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.chartDocument.Print();
            }
        }

        private void ChartDocument_PrintPage(object sender, PrintPageEventArgs ev)
        {
            string[] lines = new string[9];
            lines[1] = "ISIN:           " + this.broker.Isin;
            lines[2] = "WKN:            " + this.broker.Wkn;
            lines[3] = "Symbol:         " + this.broker.Symbol;
            lines[4] = "Gattung:        " + this.broker.Type;
            lines[5] = "Land:           " + this.broker.Country;
            lines[6] = "Währung:        " + this.broker.Currency;
            lines[7] = "Aktueller Wert: " + this.broker.ActualValue;
            lines[8] = "Aktuelle Zeit:  " + this.broker.DateTime;

            float linesPerPage = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            float yPosition = topMargin;
            string line = null;
            Font printFont = new Font(new FontFamily("Consolas"), 14f);
            SolidBrush myBrush = new SolidBrush(Color.DarkBlue);

            ev.Graphics.DrawString(this.broker.Name, printFont, myBrush, leftMargin + 60, yPosition, new StringFormat());
            yPosition += 30f;
            printFont = new Font(new FontFamily("Consolas"), 11f);

            // Work out the number of lines per page, using the MarginBounds.
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
            // Iterate over the string using the StringReader, printing each line.
            while (count < linesPerPage && count < lines.Length)
            {
                line = lines[count];
                // calculate the next line position based on the height of the font according to the printing device
                yPosition += printFont.GetHeight(ev.Graphics);
                // draw the next line in the rich edit control
                ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
                line = null;
            }
            // Add image
            yPosition += 40f;
            ev.Graphics.DrawImage(this.image.Image, leftMargin, yPosition);

            // If there are more lines, print another page.
            if (line != null)
            {
                ev.HasMorePages = true;
            }
            else
            {
                ev.HasMorePages = false;
            }
            myBrush.Dispose();
        }
    }
}
